package com.bort.system.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.bort.system.model.Role;
import com.bort.system.service.RoleService;
import com.bort.system.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @Resource
    RoleService roleService;

    @GetMapping("/list")
    public Result index(@RequestParam(value = "name",required = false)String name,
                        @RequestParam(value = "size" ,defaultValue = "10")Integer size,
                        @RequestParam(value = "current",defaultValue = "1")Integer current) {
        Result<List<Role>> result = new Result<>();
        result.setData(roleService.selectAll(name));
        return result;
    }

    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        if (role.getId() != null && role.getId() > 0) {
            roleService.updateById(role);
        } else {
            roleService.add(role);
        }
        return new Result();
    }

    @RequestMapping(value = "/saveRoleMenus", method = RequestMethod.POST)
    public Result saveRoleMenus(@RequestBody JSONObject params) {
        roleService.saveRoleMenus(params);
        return new Result();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestParam Integer id) {
        roleService.delete(id);
        return new Result();
    }
}
